package com.wsx.sky.dubbo.service;

import com.wsx.sky.dubbo.model.DubboServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

/**
 * @author wangshuaixin
 * @Title: com.wsx.sky.dubbo.service.MongoService
 * @Description: TODO
 * @date 2018/12/11
 */
@Component
public class MongoService {

    @Autowired
    private MongoTemplate mongoTemplate;


    public DubboServer getOrSave(String name) {
        Query query = new Query();
        query.addCriteria(Criteria.where("name").is(name));
        DubboServer dubboServer = mongoTemplate.findOne(query, DubboServer.class, "dubbo_server");
        if (null == dubboServer) {
            dubboServer = new DubboServer();
            dubboServer.setName(name);
            mongoTemplate.save(dubboServer, "dubbo_server");
        }
        return dubboServer;
    }
}
